package com.yunhe.exaple.dao;

import com.yunhe.exaple.entity.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    public List<Student> findForPage(int page,int pageSize){
        ArrayList<Student> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "root123");
            String sql = " select id,user_name,touxiang from student limit ?,? ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,(page - 1) * pageSize);
            stmt.setObject(2,pageSize);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                int id = rs.getInt("id");
                String userName = rs.getString("user_name");
                String touxiang = rs.getString("touxiang");
                list.add(new Student(id,userName,touxiang));
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }
}
